package com.jy.wms.service;

import PTLwebService.Inventory;
import PTLwebService.Result;
import PTLwebService.Service1;
import PTLwebService.Service1Soap;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.PropertyUtil;
import com.jy.wms.common.RedisUtils;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.dao.WmsPickingMapper;
import org.apache.log4j.Logger;
import feign.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.math.BigDecimal;
import java.security.acl.Owner;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;


/**
 * @ClassName: WmsPickingService.java
 * @Description: 拣货管理/发运管理
 * @Author syh
 * @Date Wed Oct 25 18:25:38 CST 2017
 */

@Service
public class WmsPickingService extends BaseService<WmsPickingMapper,WmsPicking> {
    protected static Logger log = Logger.getLogger(WmsPickingService.class);
    @Autowired
    RedisUtils redisUtils;

    /**
     * 下发拣货任务信息验证
     * @param sshzid
     * @param ckid
     * @param wmsPickingList
     * @return
     */
    @BaseServiceDescribe(moduleName = "拣货管理", btnName = "下发拣货任务信息验证")
    public String inspectPick(String sshzid,String ckid,List<WmsPicking> wmsPickingList) throws Exception{
        StringBuffer stringBuffer=new StringBuffer();
        Map<String, Object> map = new HashMap<>();
        map.put("ckid",ckid);
        map.put("sshzid",sshzid);
        map.put("list",wmsPickingList);
        //查询发运明细内的货品明细信息
        List<WmsGoodsLocation> wmsGoodsLocationList=dao.serachKw(map);
        //循环库位类型
        for(WmsGoodsLocation wmsGoodsLocation:wmsGoodsLocationList){
            //查询库位类型
            String lx=dao.searchKwlx(wmsGoodsLocation.getKwid());
            //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
            if(!"1".equals(lx) && !"2".equals(lx) && !"3".equals(lx)){
                //查询货品名称
                String mc=dao.searchHpmc(wmsGoodsLocation.getHpid());
                stringBuffer.append(mc);
                stringBuffer.append(",");
            }
        }
        if(stringBuffer.toString()!=null && !"".equals(stringBuffer.toString())){
            stringBuffer.append("没有绑定拣货库位");
        }
        return stringBuffer.toString();
    }

    /**
     * 拣货
     *
     * @param sshzid
     * @param userinfo
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "拣货管理", btnName = "拣货")
    @Transactional
    public void pickWmsicking(String sshzid, UserInfo userinfo, List<String> list) throws Exception {
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        String name = userinfo.getUser().getName();//当前用户名称
        Map<String, Object> map = null;
        Map<String, Object> map2 = null;
        //判断货主管理中“是否切箱”  0不切箱 1切箱
        WmsPicking owner = dao.sechSfqx(sshzid);
        Integer jhsl = 0;//主单位数量
        //拣货状态(0,初始;1,部分拣货;2,全部拣货)
        List<String> stringList = new ArrayList<>();
        stringList.add("0");
        for (String id : list) {
            long incr=redisUtils.incr("拣货"+id);
            long ins=1;
            if(incr!=ins){
                redisUtils.remove("拣货"+id);
                throw new RuntimeException("正在等待操作请稍后刷新重试。。。");
            }
            WmsShipmentOrder wmsShipmentOrder = dao.selectDd(id);
            String fyzt = "1";
            String fyzt2 = "2";
            if (!fyzt.equals(wmsShipmentOrder.getFyzt()) && !fyzt2.equals(wmsShipmentOrder.getFyzt())) {
                redisUtils.remove("拣货"+id);
                throw new RuntimeException("请选择SO状态为部分分配或者全部分配的发运订单数据");
            } else {
                String jhzt = "0";
                String jhzt2 = "1";
                if (!jhzt.equals(wmsShipmentOrder.getJhzt())) {
                    redisUtils.remove("拣货"+id);
                    throw new RuntimeException("请选择拣货状态为初始的发运订单数据");
                } else {
                    String cdzt = "1";
                    if (!cdzt.equals(wmsShipmentOrder.getCdzt())) {
                        redisUtils.remove("拣货"+id);
                        throw new RuntimeException("请选择拆单状态为已拆单的发运订单数据");
                    } else {
                        //拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
                        String jhrwzt = "2";
                        if (jhrwzt.equals(wmsShipmentOrder.getJhrwzt())) {
                            redisUtils.remove("拣货"+id);
                            throw new RuntimeException("请选择拣货任务状态为初始/部分生成的发运订单数据");
                        } else {
                            //查询发运订单明细用订单id
                            map = new HashMap<>();
                            map.put("ssfyddid", wmsShipmentOrder.getId());
                            map.put("jhzt", stringList);
                            List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                            boolean flag = false;
                            //循环明细
                            for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                                Integer xdwys = 0;//主单位个数
                                Integer xdwzs = 0;//箱单位总数
                                Integer zsl = 0;//先定义一个总数量(用于和发运明细的主单位数量进行比对)
                                //是否装箱(0不装箱 1装箱)
                                if ("1".equals(owner.getZxqx())) {
                                    //判断是否质检
                                    if ("0".equals(wmsShipmentOrder.getZxzt())) {
                                        redisUtils.remove("拣货"+id);
                                        throw new RuntimeException(wmsShipmentOrder.getFybh() + "单号数据未装箱");
                                    } else {
                                        map = new HashMap<String, Object>();
                                        //发运明细id
                                        map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                                        //货主id
                                        map.put("sshzid", sshzid);
                                        //仓库id
                                        map.put("ckid", ckid);
                                        //拣货人
                                        map.put("jhr", "0");
                                        //拣货任务生成状态 0：未生成 1：已生成
                                        map.put("sczt", "0");
                                        //拿着订单明细id去查分配表、库存的集合数据
                                        List<WmsPicking> wmsPickingList = dao.seachKc(map);
                                        WmsPicking pick = null;//生成拣货记录的对象
                                        for (WmsPicking wmsPicking : wmsPickingList) {
                                            Integer kcsl = 0;//先定义一个库存需要减去的待拣货数量
                                            //用分配的id 查装箱的集合
                                            List<WmsBox> wmsBoxList = dao.selectZxjh(wmsPicking.getSsfpid());
                                            for (WmsBox box : wmsBoxList) {
                                                //查询到库存对象
                                                WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                                String kwid=wmsStock.getKwid();
                                                //库存数量
                                                String sl1 = wmsStock.getSl();
                                                String id2 = wmsStock.getId();
                                                //待下架数量
                                                String dxjsl1 = wmsStock.getDxjsl();
                                                String id1 = UUIDUtils.uuid();
                                                //库存id
                                                wmsStock.setId(id1);
                                                //把库位id换成集货区库位id
                                                wmsStock.setKwid(owner.getJhqkwid());
                                                //把实际装箱数量主单位 ,放到库存表的库存数量里
                                                wmsStock.setSl(box.getSl());
                                                wmsStock.setDxjsl("0");
                                                wmsStock.setDzysl("0");
                                                wmsStock.setDjsl("0");
                                                wmsStock.setPssl("0");
                                                wmsStock.setBhsl("0");
                                                map.put("WmsStock", wmsStock);
                                                Integer lhkw = dao.insetNewKc(map);//生成新库存
                                                if (lhkw > 0) {
                                                    //库存操作记录
                                                    DigestUtil.addStockHistory(id1,name,Integer.parseInt(box.getSl()),0,0,0,0,0,4,"拣货操作转移到集货库位");
                                                    //log.info("拣货操作:(生成集货区库存id:" + id1 + ",数量:" + box.getSl() + ")");
                                                } else {
                                                    redisUtils.remove("拣货"+id);
                                                    throw new RuntimeException("理货库存新增失败");
                                                }
                                                map2 = new HashMap<String, Object>();
                                                pick = new WmsPicking();
                                                pick.setId(UUIDUtils.uuid());
                                                pick.setSsfpid(wmsPicking.getSsfpid());
                                                pick.setSszxid(box.getId());
                                                pick.setSsydid(wmsShipmentOrder.getId());
                                                pick.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                                pick.setKcid(wmsPicking.getKcid());
                                                pick.setSjsl(box.getSl());
                                                pick.setJhkwid(wmsPicking.getJhkwid());
                                                pick.setJihkwid(owner.getJhqkwid());
                                                pick.setFyzt("0");
                                                pick.setJhhkcid("0");
                                                pick.setCkid(ckid);
                                                pick.setJhhkcid(wmsStock.getId());
                                                pick.setGysid(wmsStock.getGysid());
                                                pick.setHpid(wmsPicking.getHpid());
                                                pick.setHppc(wmsPicking.getHppc());
                                                pick.setScrq(wmsPicking.getScrq());
                                                map2.put("WmsPicking", pick);
                                                Integer jhls = dao.insertPick(map2);//添加拣货历史记录
                                                if (jhls > 0) {
                                                    //把现在得到的总数,到库存表里用待下架数量和库存数量各自减掉去
                                                    map = new HashMap<String, Object>();
                                                    map.put("kcid", wmsPicking.getKcid());
                                                    map.put("kcsl", box.getSl());
                                                    Integer kcdj = dao.updateSl(map);
                                                    if (kcdj > 0) {
                                                        WmsLibraryPosition wmsLibraryPosition=dao.searchKw(kwid);
                                                        //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
                                                        if("3".equals(wmsLibraryPosition.getLx())){
                                                            //扣减dps库存
                                                            /*Result result=getResult(wmsStock,Integer.valueOf(box.getSl()),wmsLibraryPosition);
                                                            if ("s".equalsIgnoreCase(result.getResult())) {
                                                                log.info("DPS库存扣减" + Integer.valueOf(box.getSl()));
                                                            } else {
                                                                redisUtils.remove("拣货"+id);
                                                                log.info("DPS库存扣减失败,DPS返回值：" +result.getResult()+"；返回明细："+result.getSMsg());
                                                                throw new RuntimeException("DPS库存扣减失败:"+result.getSMsg());
                                                            }*/
                                                        }
                                                        DigestUtil.addStockHistory(id2,name,Integer.parseInt(box.getSl()),Integer.parseInt(box.getSl()),0,0,0,0,4,"拣货操作扣减库存");
                                                        //log.info("拣货操作:(扣减库存id:" + id2 + ",库存原数量:" + sl1 + ",原待拣货数量:" + dxjsl1 + ",拣货数量:" + box.getSl() + ")");
                                                    } else {
                                                        redisUtils.remove("拣货"+id);
                                                        throw new RuntimeException("库存数量更改失败");
                                                    }
                                                } else {
                                                    redisUtils.remove("拣货"+id);
                                                    throw new RuntimeException("拣货历史记录添加失败");
                                                }
                                                Integer sl = Integer.valueOf(box.getSl());//箱里的主单位数量,也就是实际数量
                                                kcsl = sl + kcsl;//(一条分配对应一条库存,多个装箱的总数量) 循环叠加(把装箱里的实际数量循环取出来和定义的总数)
                                            }
                                            map.put("name", name);
                                            map.put("ssfpid", wmsPicking.getSsfpid());
                                            Integer fpjhr = dao.updateFp(map);//更改分配拣货人
                                            if (fpjhr > 0) {
                                            } else {
                                                redisUtils.remove("拣货"+id);
                                                throw new RuntimeException("分配数据拣货人更改失败");
                                            }
                                            Integer zxjhr = dao.updateZh(map);//更改装箱拣货人
                                            if (zxjhr > 0) {
                                            } else {
                                                redisUtils.remove("拣货"+id);
                                                throw new RuntimeException("装箱数据拣货人更改失败");
                                            }
                                            zsl = kcsl + zsl;//全部分配对应装箱的主单位数量,(一条明细对应全部装箱的主单位数量)zsl是最终用于判断的全部拣货还是部分拣货
                                        }
                                    }
                                } else if ("0".equals(owner.getZxqx())) {
                                    WmsPicking picking = null;//生成拣货记录的对象
                                    //拿着订单明细id 查分配表和库存的 联合集合数据
                                    map = new HashMap<String, Object>();
                                    map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                                    map.put("sshzid", sshzid);
                                    map.put("ckid", ckid);
                                    map.put("jhr", "0");
                                    //拣货任务生成状态 0：未生成 1：已生成
                                    map.put("sczt", "0");
                                    List<WmsPicking> wmsPickingList2 = dao.seachKc_s(map);
                                    for (WmsPicking wmsPicking : wmsPickingList2) {
                                        map2 = new HashMap<String, Object>();
                                        Integer sl = Integer.valueOf(wmsPicking.getFpsl());//箱里的主单位数量,也就是实际数量
                                        zsl = sl + zsl;//循环叠加(把箱里的实际数量循环取出来和定义的总数),zsl这个总数用于判断是全拣货还是部分拣货的
                                        //查询到库存对象
                                        WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                        String kwid=wmsStock.getKwid();
                                        //库存数量
                                        String sl1 = wmsStock.getSl();
                                        String id2 = wmsStock.getId();
                                        //待下架数量
                                        String dxjsl1 = wmsStock.getDxjsl();
                                        String id1 = UUIDUtils.uuid();
                                        //库存id
                                        wmsStock.setId(id1);
                                        //把库位id换成集货区库位id
                                        wmsStock.setKwid(owner.getJhqkwid());
                                        //把实际装箱数量主单位 ,放到库存表的库存数量里
                                        wmsStock.setSl(String.valueOf(sl));
                                        wmsStock.setDxjsl("0");
                                        wmsStock.setDzysl("0");
                                        wmsStock.setDjsl("0");
                                        wmsStock.setPssl("0");
                                        wmsStock.setBhsl("0");
                                        map2.put("WmsStock", wmsStock);
                                        //生成新库存
                                        Integer lhkw = dao.insetNewKc(map2);
                                        if (lhkw > 0) {
                                            WmsLibraryPosition wmsLibraryPosition=dao.searchKw(kwid);
                                            //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
                                            if("3".equals(wmsLibraryPosition.getLx())){
                                                //扣减dps库存
                                                /*Result result=getResult(wmsStock,sl,wmsLibraryPosition);
                                                if ("s".equals(result.getResult())) {
                                                    log.info("DPS库存扣减" + sl);
                                                } else {
                                                    redisUtils.remove("拣货"+id);
                                                    throw new ServiceException("DPS库存扣减失败");
                                                }*/
                                            }
                                            DigestUtil.addStockHistory(id1,name,sl,0,0,0,0,0,4,"拣货操作转移到集货库位");
                                            //log.info("拣货操作:(生成集货区库存id:" + id1 + ",数量:" + sl + ")");
                                        } else {
                                            redisUtils.remove("拣货"+id);
                                            throw new RuntimeException("理货库存新增失败");
                                        }
                                        picking = new WmsPicking();
                                        picking.setId(UUIDUtils.uuid());
                                        picking.setSsfpid(wmsPicking.getSsfpid());
                                        picking.setSszxid("0");
                                        picking.setSsydid(wmsShipmentOrder.getId());
                                        picking.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                        picking.setKcid(wmsPicking.getKcid());
                                        picking.setSjsl(wmsPicking.getFpsl());
                                        picking.setJhkwid(wmsPicking.getJhkwid());
                                        picking.setJihkwid(owner.getJhqkwid());
                                        picking.setFyzt("0");
                                        picking.setJhhkcid("0");
                                        picking.setCkid(ckid);
                                        picking.setJhhkcid(wmsStock.getId());
                                        picking.setGysid(wmsStock.getGysid());
                                        picking.setHpid(wmsPicking.getHpid());
                                        picking.setHppc(wmsPicking.getHppc());
                                        picking.setScrq(wmsPicking.getScrq());
                                        map2.put("WmsPicking", picking);
                                        //添加拣货历史记录
                                        Integer jhls = dao.insertPick(map2);
                                        if (jhls > 0) {
                                            map2.put("kcid", wmsPicking.getKcid());
                                            map2.put("kcsl", sl);
                                            Integer kcdj = dao.updateSl(map2);
                                            if (kcdj > 0) {
                                                DigestUtil.addStockHistory(id2,name,sl,sl,0,0,0,0,4,"拣货操作扣减库存");
                                                //log.info("拣货操作:(扣减库存id:" + id2 + ",库存原数量:" + sl1 + ",原待拣货数量:" + dxjsl1 + ",拣货数量:" + sl + ")");
                                            } else {
                                                redisUtils.remove("拣货"+id);
                                                throw new RuntimeException("库存数量更改失败");
                                            }
                                        } else {
                                            redisUtils.remove("拣货"+id);
                                            throw new RuntimeException("拣货历史记录添加失败");
                                        }
                                        map2.put("name", name);
                                        map2.put("ssfpid", wmsPicking.getSsfpid());
                                        Integer fpjhr = dao.updateFp(map2);//更改分配拣货人
                                        if (fpjhr > 0) {
                                        } else {
                                            redisUtils.remove("拣货"+id);
                                            throw new RuntimeException("分配数据拣货人更改失败");
                                        }
                                    }
                                } else {
                                    redisUtils.remove("拣货"+id);
                                    throw new RuntimeException("货主是否装箱状态有误");
                                }
                                // 用货品id查这个箱里的货品对应的包装明细类型为2的箱数量//在货品放这个箱里能放多少个
                                map = new HashMap<String, Object>();
                                map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                                map.put("bzlx", "2");
                                WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                                map.put("bzlx", "0");
                                WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                                //箱主单位整数（箱数）
                                xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                                //箱主单位余数（个数）
                                xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                                String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                                Integer mxfyzt;
                                map = new HashMap<String, Object>();
                                map.put("id", wmsShipmentOrderDetailed.getId());
                                map.put("jhxdwsl", jhxdwsl);
                                map.put("zsl", zsl);
                                //把现在得到的总数,拿着和订单明细里的数量进行比对,如果相等是全部检出,如果小于订单明细里的数量是部分检出
                                Integer fpsl = Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                                if (zsl.equals(fpsl)) {
                                    map.put("fymxzt", "4");
                                    map.put("jhzt", "2");
                                } else {
                                    flag = true;
                                    map.put("fymxzt", "3");
                                    map.put("jhzt", "1");
                                }
                                //改发运明细状态
                                mxfyzt = dao.updateFymxzt(map);
                                if (mxfyzt > 0) {
                                } else {
                                    redisUtils.remove("拣货"+id);
                                    throw new RuntimeException("发运明细状态更改失败");
                                }
                            }
                            Integer ddfyzt;
                            map = new HashMap<String, Object>();
                            map.put("id", wmsShipmentOrder.getId());
                            if (flag == false) {
                                map.put("jhzt", "2");
                            } else {
                                map.put("jhzt", "1");
                            }
                            //发运订单里的拣货状态
                            ddfyzt = dao.updateFy(map);
                            if (ddfyzt > 0) {
                                redisUtils.remove("拣货"+id);
                            } else {
                                redisUtils.remove("拣货"+id);
                                throw new RuntimeException("发运状态更改失败");
                            }
                        }
                    }
                }
            }
        }
        throw new SucessException("拣货成功");
    }

    /**
     * 追加拣货
     * @param sshzid
     * @param userinfo
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "追加拣货管理",btnName = "追加拣货")
    @Transactional
    public void AddPicking( String sshzid,UserInfo userinfo,List<String> list) throws Exception {
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        String name = userinfo.getUser().getName();//当前用户名称
        Map<String, Object> map = null;
        Map<String, Object> map2 = null;
        //判断货主管理中“是否切箱” zxqx 0不切箱 1切箱,  dcfh是否允许多次发货 0不允许 1允许 默认1
        WmsPicking owner = dao.sechSfqx(sshzid);
        //拣货状态(0,初始;1,部分拣货;2,全部拣货)
        List<String> stringList = new ArrayList<>();
        stringList.add("1");
        stringList.add("0");
        if ("1".equals(owner.getDcfh())) {
            for (String id : list) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(id);
                String jhzt = "1";
                if (!jhzt.equals(wmsShipmentOrder.getJhzt())) {
                    throw new ServiceException("请选择拣货状态为部分拣货的发运订单数据");
                } else {
                    //查询发运订单明细用订单id
                    map = new HashMap<>();
                    map.put("ssfyddid", wmsShipmentOrder.getId());
                    map.put("jhzt", stringList);
                    List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                    Integer flag = 0;
                    //循环明细
                    for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                        Integer xdwys = 0;//主单位个数
                        Integer xdwzs = 0;//箱单位总数
                        Integer zsl = 0;//先定义一个总数量(用于和发运明细的主单位数量进行比对)
                        if ("1".equals(owner.getZxqx())) {
                            map = new HashMap<String, Object>();
                            map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                            map.put("sshzid", sshzid);
                            map.put("ckid", ckid);
                            map.put("jhr", "0");
                            //拣货任务生成状态 0：未生成 1：已生成
                            map.put("sczt", "0");
                            //拿着订单明细id去查分配表、库存的集合数据
                            List<WmsPicking> wmsPickingList = dao.seachKc(map);
                            WmsPicking pick = null;//生成拣货记录的对象
                            for (WmsPicking wmsPicking : wmsPickingList) {
                                Integer kcsl = 0;//先定义一个库存需要减去的待拣货数量
                                //用分配的id 查装箱的集合
                                List<WmsBox> wmsBoxList = dao.selectZxjh(wmsPicking.getSsfpid());
                                for (WmsBox box : wmsBoxList) {
                                    //查询到库存对象
                                    WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                    //库存数量
                                    String sl1 = wmsStock.getSl();
                                    String id2 = wmsStock.getId();
                                    //待下架数量
                                    String dxjsl1 = wmsStock.getDxjsl();
                                    String id1 = UUIDUtils.uuid();
                                    //库存id
                                    wmsStock.setId(id1);
                                    //把库位id换成集货区库位id
                                    wmsStock.setKwid(owner.getJhqkwid());
                                    //把实际装箱数量主单位 ,放到库存表的库存数量里
                                    wmsStock.setSl(String.valueOf(box.getSl()));
                                    wmsStock.setDxjsl("0");
                                    wmsStock.setDzysl("0");
                                    wmsStock.setDjsl("0");
                                    wmsStock.setPssl("0");
                                    wmsStock.setBhsl("0");
                                    map.put("WmsStock", wmsStock);
                                    Integer lhkw = dao.insetNewKc(map);//生成新库存
                                    if (lhkw > 0) {
                                        log.info("拣货操作:(生成集货区库存id:" + id1 + ",数量:" + box.getSl() + ")");
                                    } else {
                                        throw new ServiceException("理货库存新增失败");
                                    }
                                    map2 = new HashMap<String, Object>();
                                    pick = new WmsPicking();
                                    pick.setId(UUIDUtils.uuid());
                                    pick.setSsfpid(wmsPicking.getSsfpid());
                                    pick.setSszxid(box.getId());
                                    pick.setSsydid(wmsShipmentOrder.getId());
                                    pick.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                    pick.setKcid(wmsPicking.getKcid());
                                    pick.setSjsl(box.getSl());
                                    pick.setJhkwid(wmsPicking.getJhkwid());
                                    pick.setJihkwid(owner.getJhqkwid());
                                    pick.setFyzt("0");
                                    pick.setJhhkcid("0");
                                    pick.setCkid(ckid);
                                    pick.setJhhkcid(wmsStock.getId());
                                    pick.setGysid(wmsStock.getGysid());
                                    pick.setHpid(wmsPicking.getHpid());
                                    pick.setHppc(wmsPicking.getHppc());
                                    pick.setScrq(wmsPicking.getScrq());
                                    map2.put("WmsPicking", pick);
                                    Integer jhls = dao.insertPick(map2);//添加拣货历史记录
                                    if (jhls > 0) {
                                        //把现在得到的总数,到库存表里用待下架数量和库存数量各自减掉去
                                        map = new HashMap<String, Object>();
                                        map.put("kcid", wmsPicking.getKcid());
                                        map.put("kcsl", box.getSl());
                                        Integer kcdj = dao.updateSl(map);
                                        if (kcdj > 0) {
                                            log.info("拣货操作:(扣减库存id:" + id2 + ",库存原数量:" + sl1 + ",原待拣货数量:" + dxjsl1 + ",拣货数量:" + box.getSl() + ")");
                                        } else {
                                            throw new ServiceException("库存数量更改失败");
                                        }
                                    } else {
                                        throw new ServiceException("拣货历史记录添加失败");
                                    }
                                    Integer sl = Integer.valueOf(box.getSl());//箱里的主单位数量,也就是实际数量
                                    kcsl = sl + kcsl;//(一条分配对应一条库存,多个装箱的总数量) 循环叠加(把装箱里的实际数量循环取出来和定义的总数)
                                }
                                map.put("name", name);
                                map.put("ssfpid", wmsPicking.getSsfpid());
                                Integer fpjhr = dao.updateFp(map);//更改分配拣货人
                                if (fpjhr > 0) {
                                } else {
                                    throw new ServiceException("分配数据拣货人更改失败");
                                }
                                Integer zxjhr = dao.updateZh(map);//更改装箱拣货人
                                if (zxjhr > 0) {
                                } else {
                                    throw new ServiceException("装箱数据拣货人更改失败");
                                }
                                zsl = kcsl + zsl;//全部分配对应装箱的主单位数量,(一条明细对应全部装箱的主单位数量)zsl是最终用于判断的全部拣货还是部分拣货
                            }
                        } else {
                            WmsPicking picking = null;//生成拣货记录的对象
                            //拿着订单明细id 查分配表和库存的 联合集合数据
                            map = new HashMap<String, Object>();
                            map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                            map.put("sshzid", sshzid);
                            map.put("ckid", ckid);
                            map.put("jhr", "0");
                            //拣货任务生成状态 0：未生成 1：已生成
                            map.put("sczt", "0");
                            List<WmsPicking> wmsPickingList2 = dao.seachKc_s(map);
                            for (WmsPicking wmsPicking : wmsPickingList2) {
                                map2 = new HashMap<String, Object>();
                                Integer sl = Integer.valueOf(wmsPicking.getFpsl());//箱里的主单位数量,也就是实际数量
                                zsl = sl + zsl;//循环叠加(把箱里的实际数量循环取出来和定义的总数),zsl这个总数用于判断是全拣货还是部分拣货的

                                //查询到库存对象
                                WmsStock wmsStock = dao.selectKc(wmsPicking.getKcid());
                                //库存数量
                                String sl1 = wmsStock.getSl();
                                String id2 = wmsStock.getId();
                                //待下架数量
                                String dxjsl1 = wmsStock.getDxjsl();
                                String id1 = UUIDUtils.uuid();
                                //库存id
                                wmsStock.setId(id1);
                                //把库位id换成集货区库位id
                                wmsStock.setKwid(owner.getJhqkwid());
                                //把实际装箱数量主单位 ,放到库存表的库存数量里
                                wmsStock.setSl(String.valueOf(sl));
                                wmsStock.setDxjsl("0");
                                wmsStock.setDzysl("0");
                                wmsStock.setDjsl("0");
                                wmsStock.setPssl("0");
                                wmsStock.setBhsl("0");
                                map2.put("WmsStock", wmsStock);
                                Integer lhkw = dao.insetNewKc(map2);//生成新库存
                                if (lhkw > 0) {
                                    log.info("拣货操作:(生成集货区库存id:" + id1 + ",数量:" + sl + ")");
                                } else {
                                    throw new ServiceException("理货库存新增失败");
                                }
                                picking = new WmsPicking();
                                picking.setId(UUIDUtils.uuid());
                                picking.setSsfpid(wmsPicking.getSsfpid());
                                picking.setSszxid("0");
                                picking.setSsydid(wmsShipmentOrder.getId());
                                picking.setSsfymxid(wmsShipmentOrderDetailed.getId());
                                picking.setKcid(wmsPicking.getKcid());
                                picking.setSjsl(wmsPicking.getFpsl());
                                picking.setJhkwid(wmsPicking.getJhkwid());
                                picking.setJihkwid(owner.getJhqkwid());
                                picking.setFyzt("0");
                                picking.setJhhkcid("0");
                                picking.setCkid(ckid);
                                picking.setJhhkcid(wmsStock.getId());
                                picking.setGysid(wmsStock.getGysid());
                                picking.setHpid(wmsPicking.getHpid());
                                picking.setHppc(wmsPicking.getHppc());
                                picking.setScrq(wmsPicking.getScrq());
                                map2.put("WmsPicking", picking);
                                Integer jhLs = dao.insertPick(map2);//添加拣货历史记录
                                Integer jhLs2 = 0;
                                if (jhLs > jhLs2) {
                                    map2.put("kcid", wmsPicking.getKcid());
                                    map2.put("kcsl", sl);
                                    //扣减库存
                                    Integer kcdj = dao.updateSl(map2);
                                    if (kcdj > 0) {
                                        log.info("拣货操作:(扣减库存id:" + id2 + ",库存原数量:" + sl1 + ",原待拣货数量:" + dxjsl1 + ",拣货数量:" + sl + ")");
                                    } else {
                                        throw new ServiceException("库存数量更改失败");
                                    }
                                } else {
                                    throw new ServiceException("拣货历史记录添加失败");
                                }
                                map2.put("name", name);
                                map2.put("ssfpid", wmsPicking.getSsfpid());
                                Integer fpjhr = dao.updateFp(map2);//更改分配拣货人
                                if (fpjhr > 0) {
                                } else {
                                    throw new ServiceException("分配数据拣货人更改失败");
                                }
                                /*Integer zxjhr = dao.updateZh(map2);//更改装箱拣货人*//*
                                if (zxjhr > 0) {
                                } else {
                                    throw new ServiceException("装箱数据拣货人更改失败");
                                }*/
                            }
                        }
                        // 用货品id查这个箱里的货品对应的包装明细类型为2的箱数量//在货品放这个箱里能放多少个
                        map = new HashMap<String, Object>();
                        map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                        map.put("bzlx", "2");
                        WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                        map.put("bzlx", "0");
                        WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                        //每次累计明细里的拣货数量
                        zsl = zsl + Integer.parseInt(wmsShipmentOrderDetailed.getJhsl());
                        //箱主单位整数（箱数）
                        xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                        //箱主单位余数（个数）
                        xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                        String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                        Integer mxfyzt=1;
                        map = new HashMap<String, Object>();
                        map.put("id", wmsShipmentOrderDetailed.getId());
                        map.put("jhxdwsl", jhxdwsl);
                        map.put("zsl", zsl);
                        //把现在得到的总数,拿着和订单明细里的数量进行比对,如果相等是全部检出,如果小于订单明细里的数量是部分检出
                        Integer fpsl = Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                        if (zsl.equals(fpsl)) {
                            flag = 4;
                            map.put("fymxzt", "4");
                            map.put("jhzt", "2");
                            mxfyzt = dao.updateFymxzt(map);//改发运明细状态为全部拣货
                        } else if(zsl.equals(0)){
                            flag = 1;
                        }else if(zsl<fpsl){
                            flag = 3;
                            map.put("fymxzt", "3");
                            map.put("jhzt", "1");
                            mxfyzt = dao.updateFymxzt(map);//改发运明细状态为部分拣货
                        }
                        if (mxfyzt > 0) {
                        } else {
                            throw new ServiceException("发运明细状态更改失败");
                        }
                    }
                    Integer ddfyzt=1;
                    map = new HashMap<String, Object>();
                    map.put("id", wmsShipmentOrder.getId());
                    if (flag.equals(4)) {
                        map.put("jhzt", "2");
                        ddfyzt = dao.updateFy(map);//发运订单里的状态改为全部拣货
                    } else if(flag.equals(3)){
                        map.put("jhzt", "1");
                        ddfyzt = dao.updateFy(map);//发运订单里的状态改为部分拣货
                    }
                    if (ddfyzt > 0) {
                    } else {
                        throw new ServiceException("发运状态更改失败");
                    }
                }
            }
            throw new SucessException("追加拣货成功");
        } else {
            throw new ServiceException("请选择货主管理中'是否允许多次发货'为允许的数据");
        }
    }

    /**
     * 查询发运订单信息(生成拣货任务专用)
     *
     * @param wmsPicking
     * @return
     * @throws Exception
     */
    public List<WmsPicking> pickingTaskList(WmsPicking wmsPicking) throws Exception {
        try {
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(wmsPicking.getCjsj_start()!=null && wmsPicking.getCjsj_start()!=""){
                Date date =new Date(new Long(wmsPicking.getCjsj_start()));
                wmsPicking.setCjsj_start(format.format(date));
            }
            Calendar c=Calendar.getInstance();
            if (wmsPicking.getCjsj_end() != null && wmsPicking.getCjsj_end() != "") {
                c.setTime(new Date(Long.valueOf(wmsPicking.getCjsj_end())));
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                wmsPicking.setCjsj_end(format.format(c.getTime()));
            }
            Map<String, Object> map = new HashMap();
            Map<String, Object> map1;
            //查询货主信息
            WmsPicking wmsPick = dao.sechSfqx(wmsPicking.getSshzid());
            //发运状态(1、部分分配2、全部分配)
            List<String> fyztList = new ArrayList<>();

            fyztList.add("1");
            fyztList.add("2");
            //map.put("fyztList", fyztList);
            wmsPicking.setFyztList(fyztList);
            List<String> zxztList = null;
            //查看货主是否切箱
            if ("0".equals(wmsPick.getZxqx())) {
                zxztList = null;
            } else if ("1".equals(wmsPick.getZxqx())) {
                //装箱状态（0、未装箱 1、部分装箱 2、全部装箱）
                zxztList = new ArrayList<>();
                zxztList.add("1");
                zxztList.add("2");
            }
            //map.put("zxztList", zxztList);
            wmsPicking.setZxztList(zxztList);
            //判断数据显示的内容
            List<WmsPicking> list = new ArrayList<>();
            map.put("wmsPicking", wmsPicking);
            List<WmsPicking> wmsPickingList = dao.pickingTaskList(map);
            for (WmsPicking wmsPicking2 : wmsPickingList) {
                map1 = new HashMap();
                map1.put("ssydid", wmsPicking2.getSsydid());
                //查询发运订单下的分配数据
                List<WmsDistribution> wmsDistributionList = dao.searchDistribution(map1);
                if (wmsDistributionList.size() > 0) {
                    list.add(wmsPicking2);
                }
            }
            /*if (list.size() > 0) {
                //开始的数量
                Integer start = wmsPicking.getRows() * (wmsPicking.getPage() - 1);
                //结束的数量
                Integer end = wmsPicking.getRows() * wmsPicking.getPage();
                if (end > list.size()) {
                    end = list.size();
                }
                list = list.subList(start, end);
            }*/
            return list;
        } catch (Exception e) {
            throw new ServiceException("查询发运订单信息失败", e);
        }
    }

    /**
     * 生成拣货任务信息(生成拣货任务专用)
     *
     * @param wmsPickingList
     * @param hzid
     * @param ckid
     */
    @BaseServiceDescribe(moduleName = "拣货任务", btnName = "生成拣货任务")
    @Transactional
    public void createPickingTask(List<WmsPicking> wmsPickingList, String hzid, String hzmc, String ckid) throws Exception {
        //记录pda添加数据(用于回滚)
        List<WmsPickingTaskPda> wmsPickingTaskPdaList1 = new ArrayList<>();
        //记录Dps添加数据(用于回滚)
        List<WmsPickingTaskDps> wmsPickingTaskDpsList1 = new ArrayList<>();
        //记录分配添加数据(用于回滚)
        List<WmsDistribution> wmsDistributionList1 = new ArrayList<>();
        //记录发运订单数据(用于回滚)
        List<WmsShipmentOrder> wmsShipmentOrderList1 = new ArrayList<>();
        WmsPdaMsg wmsPdaMsg = null;
        //发运明细的发运状态(1、部分分配2、全部分配)
        List<String> fyztList = new ArrayList<>();
        fyztList.add("1");
        fyztList.add("2");
        WmsDistribution wmsDistribution = null;
        Map<String, Object> map = null;
        //查询货主信息
        WmsOwner wmsOwner = dao.searchWmsOwner(hzid);
        //是否装箱 0不装箱 1装箱
        String zxqx = wmsOwner.getZxqx();
        //是否对接DPS 0无对接 1已对接
        String djdps = wmsOwner.getDjdps();
        //是否对接PDA 0无对接 1已对接
        String djpda = wmsOwner.getDjpda();
        Integer msg = 0;
        //(装箱,对接DPS,对接PDA)
        if ("1".equals(zxqx) && "1".equals(djdps) && "1".equals(djpda)) {
            //散装明细写入中间数据库,整箱明细写入到PDA拣货任务表(查询装箱表)
            for (WmsPicking wmsPicking : wmsPickingList) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(wmsPicking.getSsydid());
                //判断发运订单是否全部拣货:拣货状态（0、初始 1、部分拣货 2、全部拣货）
                if ("2".equals(wmsShipmentOrder.getJhzt())) {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("发运订单已全部拣货,不可生成拣货任务");
                } else {
                    //整散类型 0整箱 1散箱
                    String zslx = "0";
                    map = new HashMap<>();
                    map.put("zslx", zslx);
                    map.put("ydid", wmsPicking.getSsydid());
                    //查询发运订单下面的装箱的整箱数据
                    List<WmsPickingTaskPda> wmsPickingTaskPdaList = dao.searchBox(map);
                    //保存Pda
                    msg = savePda(wmsPickingTaskPdaList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, wmsOwner, map, wmsShipmentOrderList1, wmsPdaMsg);
                    //整散类型 0整箱 1散箱
                    zslx = "1";
                    map = new HashMap<>();
                    map.put("zslx", zslx);
                    map.put("ydid", wmsPicking.getSsydid());
                    //查询发运订单下面的装箱的散箱数据
                    List<WmsPickingTaskDps> wmsPickingTaskDpsList = dao.searchBox2(map);
                    log.info("wmsPickingTaskDpsList集合的大小:"+wmsPickingTaskDpsList.size());
                    //保存DPS
                    saveDps(wmsPickingTaskDpsList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrderList1, hzmc);
                    //更改发运订单拣货任务生成状态
                    updateShipmentOrder(wmsPickingTaskDpsList1, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrder, wmsShipmentOrderList1);
                    //PDA消息推送
                    if (msg > 0) {
                        saveMsg(wmsPdaMsg, map, wmsPicking);
                    }
                }
            }
        } else if ("0".equals(djdps) && "0".equals(djpda)) {
            //无pda,无dps不能进行拣货任务操作
            throw new ServiceException("货主无对接PDA、无对接DPS不可进行拣货任务操作");
        } else if ("1".equals(zxqx) && ("1".equals(djdps) || "1".equals(djpda))) {
            //装箱(查询装箱表)
            for (WmsPicking wmsPicking : wmsPickingList) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(wmsPicking.getSsydid());
                //判断发运订单是否全部拣货:拣货状态（0、初始 1、部分拣货 2、全部拣货）
                if ("2".equals(wmsShipmentOrder.getJhzt())) {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("发运订单已全部拣货,不可生成拣货任务");
                } else {
                    //查询装箱明细
                    if ("1".equals(djdps)) {
                        map = new HashMap<>();
                        map.put("zslx", null);
                        map.put("ydid", wmsPicking.getSsydid());
                        //有DPS,保存进DPS(无PDA)
                        List<WmsPickingTaskDps> wmsPickingTaskDpsList = dao.searchBox2(map);
                        //保存DPS
                        saveDps(wmsPickingTaskDpsList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrderList1, hzmc);
                    } else if ("1".equals(djpda)) {
                        map = new HashMap<>();
                        map.put("zslx", null);
                        map.put("ydid", wmsPicking.getSsydid());
                        //无DPS,保存进PDA
                        List<WmsPickingTaskPda> wmsPickingTaskPdaList = dao.searchBox(map);
                        //保存Pda
                        msg = savePda(wmsPickingTaskPdaList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, wmsOwner, map, wmsShipmentOrderList1, wmsPdaMsg);
                    }
                    //更改发运订单拣货任务生成状态
                    updateShipmentOrder(wmsPickingTaskDpsList1, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrder, wmsShipmentOrderList1);
                    //PDA消息推送
                    if (msg > 0) {
                        saveMsg(wmsPdaMsg, map, wmsPicking);
                    }
                }
            }
        } else if ("0".equals(zxqx) && ("1".equals(djdps) || "1".equals(djpda))) {
            for (WmsPicking wmsPicking : wmsPickingList) {
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(wmsPicking.getSsydid());
                //未装箱(查询分配表)
                map = new HashMap<>();
                map.put("fyztList", fyztList);
                map.put("ssfyddid", wmsPicking.getSsydid());
                List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.orderDetailed(map);
                for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                    map.put("fyddmxid", wmsShipmentOrderDetailed.getId());
                    //拣货任务生成状态 0：未生成 1：已生成
                    map.put("sczt", "0");
                    //拣货人(默认0)
                    map.put("jhr", "0");
                    //装箱状态 0未装箱 1已装箱
                    map.put("zxzt", "0");
                    if ("1".equals(djdps)) {
                        //查询发运订单下面的分配数据(DPS)
                        List<WmsPickingTaskDps> wmsDistributionList = dao.queryDistribution2(map);
                        //保存DPS
                        saveDps(wmsDistributionList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrderList1, hzmc);
                    } else if ("1".equals(djpda)) {
                        //查询发运订单下面的分配数据(PDA)
                        List<WmsPickingTaskPda> wmsDistributionList = dao.queryDistribution(map);
                        //保存Pda
                        msg = savePda(wmsDistributionList, wmsPicking, wmsPickingTaskDpsList1, wmsDistribution, wmsDistributionList1, wmsPickingTaskPdaList1, wmsOwner, map, wmsShipmentOrderList1, wmsPdaMsg);
                    }
                }
                //更改发运订单拣货任务生成状态
                updateShipmentOrder(wmsPickingTaskDpsList1, wmsDistributionList1, wmsPickingTaskPdaList1, map, wmsShipmentOrder, wmsShipmentOrderList1);
                //PDA消息推送
                if (msg > 0) {
                    saveMsg(wmsPdaMsg, map, wmsPicking);
                }
            }
        }
        throw new SucessException("生成拣货任务成功");
    }

    /**
     * PDA消息推送
     *
     * @param wmsPdaMsg
     * @param map
     * @param wmsPicking
     * @throws Exception
     */
    public void saveMsg(WmsPdaMsg wmsPdaMsg, Map<String, Object> map, WmsPicking wmsPicking) throws Exception {
        //pda信息推送表添加数据
        wmsPdaMsg = new WmsPdaMsg();
        //标题
        wmsPdaMsg.setBt(wmsPicking.getFybh());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time = new Date();
        String bcTime = sdf.format(time);
        //消息
        wmsPdaMsg.setXx(bcTime + "生成拣货任务");
        //状态
        wmsPdaMsg.setZt("0");
        //人员id
        wmsPdaMsg.setRyid(wmsPicking.getRyid());
        //拣货任务消息推送到pda
        map.put("WmsPdaMsg", wmsPdaMsg);
        Integer i = dao.saveMsg(map);
        if (i <= 0) {
            throw new ServiceException("PDA拣货任务消息推送失败");
        }
    }

    /**
     * 添加DPS数据(生成拣货任务专用)
     *
     * @param wmsPickingTaskDpsList
     * @param wmsPicking
     * @param wmsPickingTaskDpsList1
     * @param wmsDistribution
     * @param wmsDistributionList1
     * @param wmsPickingTaskPdaList1
     * @param map
     * @throws Exception
     */
    public void saveDps(List<WmsPickingTaskDps> wmsPickingTaskDpsList, WmsPicking wmsPicking, List<WmsPickingTaskDps> wmsPickingTaskDpsList1, WmsDistribution wmsDistribution, List<WmsDistribution> wmsDistributionList1, List<WmsPickingTaskPda> wmsPickingTaskPdaList1, Map map, List<WmsShipmentOrder> wmsShipmentOrderList1, String hzmc) throws Exception {
        for (WmsPickingTaskDps wmsPickingTaskDps : wmsPickingTaskDpsList) {
            map = new HashMap<>();
            //出库单号(装箱存放装箱编号)
            wmsPickingTaskDps.setOrderNo(wmsPickingTaskDps.getOrderNo2());
            //客户名称
            wmsPickingTaskDps.setReceName(wmsPicking.getKhmc());
            //收货方地址
            wmsPickingTaskDps.setReceAddr(wmsPicking.getKhdz());
            //联系人(姓名)
            wmsPickingTaskDps.setRecePerson(wmsPicking.getLxrxm());
            //联系电话
            wmsPickingTaskDps.setReceTel(wmsPicking.getLxrdh());
            //状态(0：初始 1：已读取)
            wmsPickingTaskDps.setState("0");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = new Date();
            String bcTime = sdf.format(time);
            //创建时间
            wmsPickingTaskDps.setCreateDate(bcTime);
            //拣货任务id
            wmsPickingTaskDps.setId(UUIDUtils.uuid());
            map.put("WmsPickingTaskDps", wmsPickingTaskDps);
            //读取配置信息
            Properties prop = new Properties();
            InputStream inss = WmsPickingService.class.getResourceAsStream("/createBean.properties");
            prop.load(inss);
            String url_file = PropertyUtil.getProperty("yjs.url_file");
            log.info("url_file:"+url_file);
            String user = PropertyUtil.getProperty("yjs.user");
            log.info("user:"+user);
            String password = PropertyUtil.getProperty("yjs.password");
            log.info("password:"+password);
            String name = PropertyUtil.getProperty("yjs.name");
            log.info("name:"+name);
            String driver = PropertyUtil.getProperty("yjs.driver");
            log.info("driver:"+driver);
            //驱动信息
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url_file + name, user, password);
            //链接数据库
            Statement stat = conn.createStatement();
            String sql = createSql(wmsPickingTaskDps);
            log.info("执行sql:"+sql);
            PreparedStatement stmt = conn.prepareStatement(sql);
            Integer j = stmt.executeUpdate();
            //保存拣货任务进DPS
            //Integer j = dao.createPickingTask2(map);
            if (j > 0) {
                stmt.close();
                stat.close();
                conn.close();
                wmsPickingTaskDpsList1.add(wmsPickingTaskDps);
                wmsDistribution = new WmsDistribution();
                wmsDistribution.setId(wmsPickingTaskDps.getDetailedId());
                //拣货任务生成状态 0：未生成 1：已生成
                wmsDistribution.setSczt("1");
                //拣货人
                wmsDistribution.setJhr(hzmc);
                map.put("WmsDistribution", wmsDistribution);
                //更改分配生成状态
                j = dao.updateFp2(map);
                if (j > 0) {
                    wmsDistributionList1.add(wmsDistribution);
                } else {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("分配数据生成状态更改失败");
                }
            } else {
                stmt.close();
                stat.close();
                conn.close();
                black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                throw new ServiceException("拣货任务DPS添加失败");
            }
        }
    }
    /**
     * 创建sql语句
     *
     * @param wmsPickingTaskDps
     * @return
     */
    public String createSql(WmsPickingTaskDps wmsPickingTaskDps) {
        String sql = "INSERT INTO wms_picking_task_dps" +
                "( id," +
                "orderNo," +
                "receName," +
                "receAddr," +
                "recePerson," +
                "receTel," +
                "proNO," +
                "unitName," +
                "num," +
                "itemOneBoxNum," +
                "createDate," +
                "state," +
                "detailedId" +
                ")" +
                "VALUES" +
                "('" + wmsPickingTaskDps.getId() + "',"
                + "'" + wmsPickingTaskDps.getOrderNo() + "',"
                + "'" + wmsPickingTaskDps.getReceName() + "',"
                + "'" + wmsPickingTaskDps.getReceAddr() + "',"
                + "'" + wmsPickingTaskDps.getRecePerson() + "',"
                + "'" + wmsPickingTaskDps.getReceTel() + "',"
                + "'" + wmsPickingTaskDps.getProNO() + "',"
                + "'" + wmsPickingTaskDps.getUnitName() + "',"
                + "'" + wmsPickingTaskDps.getNum() + "',"
                + "'" + wmsPickingTaskDps.getItemOneBoxNum() + "',"
                + "'" + wmsPickingTaskDps.getCreateDate() + "',"
                + "'" + wmsPickingTaskDps.getState() + "',"
                + "'" + wmsPickingTaskDps.getDetailedId() + "'"
                + ")";
        return sql;
    }
    /**
     * 添加PDA数据(生成拣货任务专用)
     *
     * @param wmsPickingTaskPdaList
     * @param wmsPicking
     * @param wmsPickingTaskDpsList1
     * @param wmsDistribution
     * @param wmsDistributionList1
     * @param wmsPickingTaskPdaList1
     * @param wmsOwner
     * @param map
     * @throws Exception
     */
    public Integer savePda(List<WmsPickingTaskPda> wmsPickingTaskPdaList, WmsPicking wmsPicking, List<WmsPickingTaskDps> wmsPickingTaskDpsList1, WmsDistribution wmsDistribution, List<WmsDistribution> wmsDistributionList1, List<WmsPickingTaskPda> wmsPickingTaskPdaList1, WmsOwner wmsOwner, Map map, List<WmsShipmentOrder> wmsShipmentOrderList1, WmsPdaMsg wmsPdaMsg) throws Exception {
        Integer msg = 0;
        for (WmsPickingTaskPda wmsPickingTaskPda : wmsPickingTaskPdaList) {
            map = new HashMap<>();
            if ("0".equals(wmsOwner.getSmLpn())) {
                //清空LPN码(货主未扫描LPN)
                wmsPickingTaskPda.setMpbh("");
            }
            if ("0".equals(wmsOwner.getCmhpm())) {
                //清空货品码(货主未扫描货品码)
                wmsPickingTaskPda.setHpm("");
            } else {
                //货品编号放入货品码
                wmsPickingTaskPda.setHpm(wmsPickingTaskPda.getHpbh());
            }
            //发运订单id
            wmsPickingTaskPda.setFyid(wmsPicking.getSsydid());
            //发运编号
            wmsPickingTaskPda.setFybh(wmsPicking.getFybh());
            //联系人id
            wmsPickingTaskPda.setRyid("," + wmsPicking.getRyid() + ",");
            //货主id
            wmsPickingTaskPda.setSshzid(wmsOwner.getId());
            //仓库id
            wmsPickingTaskPda.setCkid(wmsOwner.getCkid());
            //集货库位id
            wmsPickingTaskPda.setJhkwid(wmsOwner.getJhqkwid());
            //集货库位名称
            wmsPickingTaskPda.setJhkw(wmsOwner.getShow_jhqkw());
            WmsPickingTaskPda wmsPickingTaskPda2 = dao.searchKhjhkwid(wmsPicking.getSsydid());
            //客户集货库位id
            wmsPickingTaskPda.setKhjhkwid(wmsPickingTaskPda2.getKhjhkwid());
            //客户集货库位名称
            wmsPickingTaskPda.setKhjhkwmc(wmsPickingTaskPda2.getKhjhkwmc());
            //对接dps
            wmsPickingTaskPda.setDjdps(wmsOwner.getDjdps());
            //0 存储库位 1件拣货库位 2箱拣货库位 3箱/件拣货库位 4暂存库位 5集货库位 6组装工作库位
            if (!"1".equals(wmsPickingTaskPda.getKwlx()) && !"2".equals(wmsPickingTaskPda.getKwlx()) && !"3".equals(wmsPickingTaskPda.getKwlx())) {
                wmsPickingTaskPda.setJhkwlx("0");
            } else {
                wmsPickingTaskPda.setJhkwlx("1");
            }
            if (wmsPickingTaskPda.getGg() != null) {
            } else {
                wmsPickingTaskPda.setGg("");
            }
            map.put("WmsPickingTaskPda", wmsPickingTaskPda);
            //保存拣货任务进pda
            Integer i = dao.createPickingTask(map);
            if (i > 0) {
                wmsPickingTaskPdaList1.add(wmsPickingTaskPda);
                wmsDistribution = new WmsDistribution();
                wmsDistribution.setId(wmsPickingTaskPda.getFpid());
                //拣货任务生成状态 0：未生成 1：已生成
                wmsDistribution.setSczt("1");
                map.put("WmsDistribution", wmsDistribution);
                //更改分配生成状态
                i = dao.updateFp2(map);
                if (i > 0) {
                    msg = 1;
                    wmsDistributionList1.add(wmsDistribution);
                } else {
                    black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                    throw new ServiceException("分配数据生成状态更改失败");
                }
            } else {
                black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
                throw new ServiceException("拣货任务PDA添加失败");
            }
        }
        return msg;
    }
    /**
     * dps专用创建sql语句
     * @return
     */
    public String createDpsSql(String state) {
        String sql = "select " +
                " id," +
                "orderNo," +
                "receName," +
                "receAddr," +
                "recePerson," +
                "receTel," +
                "proNO," +
                "unitName," +
                "num," +
                "itemOneBoxNum," +
                "createDate," +
                "downLoadDate,"+
                "state," +
                "detailedId," +
                "pickingTime" +
                " from wms_picking_task_dps " +
                "where state= "+state;
        return sql;
    }
    /**
     * dps专用创建sql语句
     * @return
     */
    public String updateDpsSql(String id) {
        String sql = "update wms_picking_task_dps" +
                " set" +
                " state='3'" +
                " where id='"+id+"'";
        return sql;
    }
    /**
     * 生成拣货任务 保存拣货信息(dps回传)
     */
    public void savePicking(){
        try {
            List<WmsPickingTaskDps> wmsPickingTaskDpsList = new ArrayList<>();
            WmsPickingTaskDps wmsPickingTaskDps = null;
            Map<String, Object> map = new HashMap<>();
            //读取配置信息
            Properties prop = new Properties();
            InputStream inss = WmsPickingService.class.getResourceAsStream("/createBean.properties");
            prop.load(inss);
            String url_file = PropertyUtil.getProperty("yjs.url_file");
            String user = PropertyUtil.getProperty("yjs.user");
            String password = PropertyUtil.getProperty("yjs.password");
            String name = PropertyUtil.getProperty("yjs.name");
            String driver = PropertyUtil.getProperty("yjs.driver");
            //驱动信息
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url_file + name, user, password);
            //链接数据库
            Statement stat = conn.createStatement();
            String sql = createDpsSql("'2'");
            System.out.println("sql:" + sql);
            PreparedStatement stmt = conn.prepareStatement(sql);
            try {
                ResultSet rs = stmt.executeQuery();
                //迭代输出ResultSet对象（依次输出每列的值）
                while (rs.next()) {
                    wmsPickingTaskDps = new WmsPickingTaskDps();
                    wmsPickingTaskDps.setId(rs.getString("id"));
                    wmsPickingTaskDps.setOrderNo(rs.getString("orderNo"));
                    wmsPickingTaskDps.setReceName(rs.getString("receName"));
                    wmsPickingTaskDps.setReceAddr(rs.getString("receAddr"));
                    wmsPickingTaskDps.setRecePerson(rs.getString("recePerson"));
                    wmsPickingTaskDps.setReceTel(rs.getString("receTel"));
                    wmsPickingTaskDps.setProNO(rs.getString("proNO"));
                    wmsPickingTaskDps.setUnitName(rs.getString("unitName"));
                    wmsPickingTaskDps.setNum(new BigDecimal(rs.getString("num")));
                    wmsPickingTaskDps.setItemOneBoxNum(Integer.parseInt(rs.getString("itemOneBoxNum")));
                    wmsPickingTaskDps.setCreateDate(rs.getString("createDate"));
                    wmsPickingTaskDps.setDownLoadDate(rs.getString("downLoadDate"));
                    wmsPickingTaskDps.setState(rs.getString("state"));
                    wmsPickingTaskDps.setDetailedId(rs.getString("detailedId"));
                    wmsPickingTaskDps.setPickingTime(rs.getString("pickingTime"));
                    String updateSql = updateDpsSql(wmsPickingTaskDps.getId());
                    System.out.println("updateSql:" + updateSql);
                    log.info("updateSql:" + updateSql);
                    PreparedStatement stmtupdate = conn.prepareStatement(updateSql);
                    Integer j = stmtupdate.executeUpdate();
                    if (j <= 0) {
                        log.info("拣货任务操作记录更改失败");
                        //throw new RuntimeException("拣货任务操作记录更改失败");
                    }
                    wmsPickingTaskDpsList.add(wmsPickingTaskDps);
                }
            }catch(Exception e){
                log.info("拣货任务内部发生异常:"+e);
                //e.printStackTrace();
            }
            stmt.close();
            stat.close();
            conn.close();
            log.info("集合大小："+wmsPickingTaskDpsList.size());
            for (WmsPickingTaskDps wmsPickingTask : wmsPickingTaskDpsList) {
                map.put("WmsPickingTaskDps", wmsPickingTask);
                //保存拣货任务进DPS
                Integer j = dao.createPickingTask2(map);
                if (j <= 0) {
                    log.info("拣货任务操作记录保存失败");
                    //throw new RuntimeException("拣货任务操作记录保存失败");
                }
                WmsDistribution wmsDistribution = dao.searchFpMx(wmsPickingTask.getDetailedId());
                if (wmsDistribution != null) {
                    //查询库存对应的供应商id
                    WmsStock wmsStock_gys=dao.selectGys(wmsDistribution.getKcid());
                    String uuid = UUIDUtils.uuid();
                    //查询发运订单明细数据(发运订单明细id)
                    WmsShipmentOrderDetailed wmsShipmentOrderDetailed = dao.searchDdMx(wmsDistribution.getSsfymxid());
                    WmsShipmentOrder wmsShipmentOrder = dao.searchDd(wmsShipmentOrderDetailed.getSsfyddid());
                    //查询货主信息
                    WmsPicking owner = dao.sechSfqx(wmsShipmentOrder.getSshzid());
                    //保存拣货数据
                    WmsPicking wmsPicking = new WmsPicking();
                    //ID
                    wmsPicking.setId(UUIDUtils.uuid());
                    //所属分配ID
                    wmsPicking.setSsfpid(wmsPickingTask.getDetailedId());
                    //“是否切箱”  0不切箱 1切箱
                    if ("1".equals(owner.getZxqx())) {
                        //查询装箱信息(分配ID)
                        List<WmsBox> wmsBoxList = dao.searchBox3(wmsPicking.getSsfpid());
                        if(wmsBoxList.size()>0){
                            WmsBox wmsBox = dao.searchBoxS(map);
                            if (wmsBox != null) {
                                wmsPicking.setSszxid(wmsBox.getId());
                            } else {
                                wmsPicking.setSszxid("0");
                            }
                        }else{
                            //装箱ID(默认0)
                            wmsPicking.setSszxid("0");
                        }
                    } else {
                        //装箱ID(默认0)
                        wmsPicking.setSszxid("0");
                    }
                    //发运订单ID
                    wmsPicking.setSsydid(wmsShipmentOrderDetailed.getSsfyddid());
                    //发运订单明细ID
                    wmsPicking.setSsfymxid(wmsShipmentOrderDetailed.getId());
                    //库存ID
                    wmsPicking.setKcid(wmsDistribution.getKcid());
                    //拣货数量(实捡数量)
                    wmsPicking.setSjsl(String.valueOf(wmsPickingTask.getNum()));
                    //拣货库位ID
                    wmsPicking.setJhkwid(wmsDistribution.getShow_kwid());
                    //集货库位ID
                    wmsPicking.setJihkwid(owner.getJhqkwid());
                    //发运状态 0未发运 1已发运
                    wmsPicking.setFyzt("0");
                    //拣货后库存ID
                    wmsPicking.setJhhkcid(uuid);
                    //仓库id
                    wmsPicking.setCkid(wmsShipmentOrderDetailed.getCkid());
                    //生产日期
                    wmsPicking.setScrq(wmsDistribution.getScrq());
                    //货品id
                    wmsPicking.setHpid(wmsDistribution.getHpid());
                    //货品批次
                    wmsPicking.setHppc(wmsDistribution.getHppc());
                    //供应商id
                    wmsPicking.setGysid(wmsStock_gys.getGysid());
                    map.put("WmsPicking", wmsPicking);
                    //添加拣货信息
                    Integer i = dao.insertPick(map);
                    if (i <= 0) {
                        log.info("拣货信息添加失败");
                        //throw new RuntimeException("拣货信息添加失败");
                    }
                    WmsDistribution wmsDistribution1 = new WmsDistribution();
                    wmsDistribution1.setJhsj(wmsPickingTask.getDownLoadDate());
                    //拣货任务生成状态 0：未生成 1：已生成
                    wmsDistribution1.setSczt("1");
                    //分配id
                    wmsDistribution1.setId(wmsPickingTask.getDetailedId());
                    map.put("WmsDistribution", wmsDistribution1);
                    //更改分配表
                    i = dao.updateFp2(map);
                    if (i <= 0) {
                        log.info("更改分配表失败");
                        //throw new RuntimeException("更改分配表失败");
                    }
                    //查询到库存对象
                    WmsStock wmsStock = dao.selectKc(wmsDistribution.getKcid());
                    //库存id
                    String id = wmsStock.getId();
                    //库存数量
                    String sl = wmsStock.getSl();
                    //库存待下架数量
                    String dxjsl = wmsStock.getDxjsl();
                    wmsStock.setId(uuid);
                    wmsStock.setDxjsl("0");
                    wmsStock.setDzysl("0");
                    wmsStock.setDjsl("0");
                    wmsStock.setPssl("0");
                    wmsStock.setBhsl("0");
                    //集货区库位id
                    wmsStock.setKwid(owner.getJhqkwid());
                    //拣货数量
                    wmsStock.setSl(String.valueOf(wmsPickingTask.getNum()));
                    map.put("WmsStock", wmsStock);
                    //生成新库存
                    i = dao.insetNewKc(map);
                    if (i <= 0) {
                        log.info("库存新增失败");
                        //throw new RuntimeException("库存新增失败");
                    }
                    log.info("DPS拣货操作:(生成集货区库存id:" + id + ",数量:" + wmsPickingTask.getNum() + ")");
                    map.put("kcid", wmsDistribution.getKcid());
                    map.put("kcsl", String.valueOf(wmsPickingTask.getNum()));
                    //更改原库存数量
                    i = dao.updateSl(map);
                    if (i <= 0) {
                        log.info("库存更改失败");
                        //throw new RuntimeException("库存更改失败");
                    }
                    log.info("DPS拣货操作(库存id:" + id + ";原库存数量:" + sl + ";原待下架数量:" + dxjsl + ")");
                    map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                    map.put("bzlx", "2");
                    //箱单位
                    WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                    map.put("bzlx", "0");
                    //主单位
                    WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                    Integer zsl = Integer.parseInt(wmsStock.getSl());
                    if ("".equals(wmsShipmentOrderDetailed.getJhsl()) || wmsShipmentOrderDetailed.getJhsl() == null) {
                        wmsShipmentOrderDetailed.setJhsl("0");
                    }
                    //数量(拣货数量+发运订单明细的分配数量)
                    zsl = zsl + Integer.parseInt(wmsShipmentOrderDetailed.getJhsl());
                    //箱主单位整数（箱数）
                    Integer xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                    //箱主单位余数（个数）
                    Integer xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                    String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                    map.put("id", wmsShipmentOrderDetailed.getId());
                    map.put("jhxdwsl", jhxdwsl);
                    map.put("zsl", zsl);
                    //取得发运订单明细分配数量
                    Integer fpsl = Integer.parseInt(wmsShipmentOrderDetailed.getFpsl());
                    if (zsl.equals(fpsl)) {
                        //4、全部拣货
                        map.put("fymxzt", "4");
                        map.put("jhzt", "2");
                    } else {
                        //3、部分拣货
                        map.put("fymxzt", "3");
                        map.put("jhzt", "1");
                    }
                    //改发运明细状态为部分拣货
                    i = dao.updateFymxzt(map);
                    if (i <= 0) {
                        log.info("发运明细状态更改失败");
                        //throw new RuntimeException("发运明细状态更改失败");
                    }
                    map.put("id", wmsShipmentOrderDetailed.getSsfyddid());
                    //统计发运订单明细数据
                    WmsShipmentOrderDetailed wmsShipmentOrderDetailed2 = dao.searchDdMx2(map);
                    if (wmsShipmentOrderDetailed2.getFpsl().equals(wmsShipmentOrderDetailed2.getJhsl())) {
                        //全部拣货
                        map.put("jhzt", "2");
                    } else {
                        //部分拣货
                        map.put("jhzt", "1");
                    }
                    //发运订单里的状态改为全部拣货
                    i = dao.updateFy(map);
                    if (i <= 0) {
                        log.info("发运状态更改失败");
                        //throw new RuntimeException("发运状态更改失败");
                    }

                } else {
                    log.info("分配明细ID数据有误");
                    //throw new RuntimeException("分配明细ID数据有误");
                }
            }
            log.info("拣货任务2操作成功");
        }catch(Exception e){
            log.info("拣货任务发生异常:"+e);
            //e.printStackTrace();
        }
        log.info("拣货任务操作成功");
        //throw new SucessException("拣货任务操作成功");
    }

    /**
     * 打印分拣单查询
     * @param wmsPrintPicking
     * @return
     */
    public List<WmsPrintPicking> printPicking(WmsPrintPicking wmsPrintPicking) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = null;
        map.put("WmsPrintPicking", wmsPrintPicking);
        List<WmsPrintPicking> wmsPrintPickingList = dao.printPicking(map);
        for (WmsPrintPicking wmsPrintPicking1 : wmsPrintPickingList) {
            /*//发运状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成)
            if (!"1".equals(wmsPrintPicking1.getFyzt()) && !"2".equals(wmsPrintPicking1.getFyzt())) {
                throw new ServiceException("请选择订单状态为部分分配或者全部分配的订单");
            } else {
                //存取拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
                if (!"0".equals(wmsPrintPicking1.getJhrwzt()) && !"1".equals(wmsPrintPicking1.getJhrwzt())) {
                    throw new ServiceException("请选择生成状态为初始或者部分生成的订单");
                } else {
                    //存取拣货状态（0、初始 1、部分拣货 2、全部拣货）
                    if (!"0".equals(wmsPrintPicking1.getJhzt()) && !"1".equals(wmsPrintPicking1.getJhzt())) {
                        throw new ServiceException("请选择拣货状态为初始或者部分拣货的订单");
                    } else {*/
                        /*WmsPrintPicking wmsPrintPicking3 = dao.printPicking2(wmsPrintPicking1.getShippingId());
                        wmsPrintPicking1.setMainUnit(wmsPrintPicking3.getMainUnit());*/
            List<WmsPrintPickingDetail> wmsPrintPickingDetail = dao.printPickingDetailed(wmsPrintPicking1.getShippingId());
            for (WmsPrintPickingDetail wmsPrintPickingDetail1 : wmsPrintPickingDetail) {
                //计算拣货数量(箱单位)
                map2 = new HashMap<String, Object>();
                map2.put("hpid", wmsPrintPickingDetail1.getGoodsId());
                map2.put("bzlx", "2");
                WmsPackingDetailed detailed = dao.searchPackingDetailed(map2);
                map2.put("bzlx", "0");
                WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map2);
                Integer total = Integer.parseInt(wmsPrintPickingDetail1.getPickMainUnit());
                //箱主单位整数（箱数）
                Integer xdwzs = total / Integer.parseInt(detailed.getZdwsl());
                //箱主单位余数（个数）
                Integer xdwys = total % Integer.parseInt(detailed.getZdwsl());
                String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                //拣货数量(箱单位)
                wmsPrintPickingDetail1.setPickBoxUnit(jhxdwsl);
            }
            wmsPrintPicking1.setWmsPrintPickingDetailList(wmsPrintPickingDetail);
        }
               /* }
            }
        }*/
        return wmsPrintPickingList;
    }

    /**
     * 打印合并分拣单查询
     *
     * @param wmsPrintPicking
     * @return
     */
    public WmsPrintPicking printMergePicking(WmsPrintPicking wmsPrintPicking) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = null;
        Map<String, Object> map3 = null;
        map.put("WmsPrintPicking", wmsPrintPicking);
        //查询合并发运订单
        WmsPrintPicking wmsPrintPicking1 = dao.printMergePicking(map);
        //获取货主集货库位
        WmsOwner wmsOwner = dao.searchWmsOwner2(wmsPrintPicking.getHzid());
        //打印合并分拣单明细查询
        List<WmsPrintPickingDetail> wmsPrintPickingDetail = dao.printMergePickingDetailed(map);
        //记录备注
        List<String> stringList = new ArrayList<>();
        StringBuffer stringBuffer = null;
        //查询备注信息
        List<WmsPrintPickingDetail> wmsPrintPickingDetaiList2 = dao.printMergePickingDetailed2(map);
        for (WmsPrintPickingDetail wmsPrintPickingDetail1 : wmsPrintPickingDetail) {
            for (WmsPrintPickingDetail wmsPrintPickingDetail2 : wmsPrintPickingDetaiList2) {
                if (wmsPrintPickingDetail1.getLibraryId().equals(wmsPrintPickingDetail2.getLibraryId()) && wmsPrintPickingDetail1.getGoodsNum().equals(wmsPrintPickingDetail2.getGoodsNum()) && wmsPrintPickingDetail1.getGoodsBatch().equals(wmsPrintPickingDetail2.getGoodsBatch()) && wmsPrintPickingDetail1.getProductionTime().equals(wmsPrintPickingDetail2.getProductionTime()) && wmsPrintPickingDetail1.getSupplierId().equals(wmsPrintPickingDetail2.getSupplierId())) {
                    stringBuffer = new StringBuffer();
                    //发运编号
                    stringBuffer.append(wmsPrintPickingDetail2.getFybh());
                    stringBuffer.append("/");
                    //客户名称
                    stringBuffer.append(wmsPrintPickingDetail2.getKhmc());
                    stringBuffer.append("/");
                    //分配数量
                    stringBuffer.append(wmsPrintPickingDetail2.getFpsl());
                    stringBuffer.append("/");
                    //集货区库位
                    stringBuffer.append(wmsOwner.getShow_jhqkw());
                    stringList.add(stringBuffer.toString());
                }
            }
            wmsPrintPickingDetail1.setStringList(stringList);
            //计算拣货数量(箱单位)
            map2 = new HashMap<String, Object>();
            map2.put("hpid", wmsPrintPickingDetail1.getGoodsId());
            map2.put("bzlx", "2");
            WmsPackingDetailed detailed = dao.searchPackingDetailed(map2);
            map2.put("bzlx", "0");
            WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map2);
            Integer total = Integer.parseInt(wmsPrintPickingDetail1.getPickMainUnit());
            //箱主单位整数（箱数）
            Integer xdwzs = total / Integer.parseInt(detailed.getZdwsl());
            //箱主单位余数（个数）
            Integer xdwys = total % Integer.parseInt(detailed.getZdwsl());
            String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
            //拣货数量(箱单位)
            wmsPrintPickingDetail1.setPickBoxUnit(jhxdwsl);
        }
        wmsPrintPicking1.setWmsPrintPickingDetailList(wmsPrintPickingDetail);
        return wmsPrintPicking1;
    }
    /**
     * 查看已完成拣货任务
     *
     * @param hzid
     * @param ckid
     * @param fyddid
     * @return
     */
    public List<WmsPickingTask> seeCompleteTask(String hzid, String ckid, String fyddid) {
        Map<String, Object> map = new HashMap<>();
        map.put("hzid", hzid);
        map.put("ckid", ckid);
        map.put("fyddid", fyddid);
        return dao.seeCompleteTask(map);
    }

    /**
     * 查看未完成拣货任务
     *
     * @param hzid
     * @param ckid
     * @param fyddid
     * @return
     */
    public List<WmsPickingTask> notCompleteTask(String hzid, String ckid, String fyddid) {
        Map<String, Object> map = new HashMap<>();
        map.put("hzid", hzid);
        map.put("ckid", ckid);
        map.put("fyddid", fyddid);
        return dao.notCompleteTask(map);
    }

    /**
     * 更改发运订单状态
     *
     * @param wmsShipmentOrder
     * @param wmsShipmentOrderList1
     */
    @Transactional
    public void updateShipmentOrder(List<WmsPickingTaskDps> wmsPickingTaskDpsList1, List<WmsDistribution> wmsDistributionList1, List<WmsPickingTaskPda> wmsPickingTaskPdaList1, Map map, WmsShipmentOrder wmsShipmentOrder, List<WmsShipmentOrder> wmsShipmentOrderList1) throws Exception {
        map = new HashMap();
        //拣货任务状态(0.初始,1.部分生成拣货任务,2.全部生成拣货任务)
        String jhrwzt = "2";
        map.put("jhrwzt", jhrwzt);
        map.put("id", wmsShipmentOrder.getId());
        //更改发运订单状态
        Integer i = dao.updateDdjhzt(map);
        if (i > 0) {
            wmsShipmentOrder.setJhrwzt(jhrwzt);
            wmsShipmentOrderList1.add(wmsShipmentOrder);
        } else {
            black(wmsDistributionList1, wmsPickingTaskPdaList1, wmsPickingTaskDpsList1, map, wmsShipmentOrderList1);
            throw new ServiceException("更改拣货任务状态失败");
        }
    }
    /**
     * 出错回滚(拣货任务专用)
     * @param wmsDistributionList
     * @param wmsPickingTaskPdaList
     * @param wmsPickingTaskDpsList
     */
    public void black(List<WmsDistribution> wmsDistributionList, List<WmsPickingTaskPda> wmsPickingTaskPdaList, List<WmsPickingTaskDps> wmsPickingTaskDpsList, Map map, List<WmsShipmentOrder> wmsShipmentOrderList1) {
        if (wmsDistributionList.size() > 0) {
            //还原分配生成状态
            for (WmsDistribution wmsDistribution : wmsDistributionList) {
                map = new HashMap();
                map.put("WmsDistribution", wmsDistribution);
                dao.updateFp2(map);
            }
        }
        if (wmsPickingTaskPdaList.size() > 0) {
            map = new HashMap();
            map.put("wmsPickingTaskPdaList", wmsPickingTaskPdaList);
            //还原PDA数据(删除pda数据)
            dao.deletePda(map);
        }
        if (wmsPickingTaskDpsList.size() > 0) {
            map = new HashMap();
            map.put("wmsPickingTaskDpsList", wmsPickingTaskDpsList);
            //还原DPS数据(删除dps数据)
            dao.deleteDps(map);
        }
        if (wmsShipmentOrderList1.size() > 0) {
            for (WmsShipmentOrder wmsShipmentOrder : wmsShipmentOrderList1) {
                map = new HashMap();
                map.put("id", wmsShipmentOrder.getId());
                map.put("jhrwzt", wmsShipmentOrder.getJhrwzt());
                //还原发运订单拣货任务状态
                dao.updateDdjhzt(map);
            }
        }
    }
    /**
     * dps扣减库存
     * @param wmsStock
     * @param sl
     * @return
     */
    @Transactional
    public Result getResult(WmsStock wmsStock, int sl,WmsLibraryPosition wmsLibraryPosition) {
        Inventory inventory;//查询库位信息
        //查询货品信息
        WmsGoods wmsGoods=dao.searchHp(wmsStock.getHpid());
        inventory=new Inventory();
        //ID
        inventory.setID(UUIDUtils.uuid());
        //库位名称
        inventory.setCLocation(wmsLibraryPosition.getMc());
        //货品编号
        inventory.setProNO(wmsGoods.getBh());
        //主单位描述
        inventory.setUnitName(wmsGoods.getZdwms());
        //修改数量
        inventory.setItemNum(sl);
        //0:扣减库存;1:增加库存
        inventory.setFlag(0);
        Service1 service1 = new Service1();
        Service1Soap service1Soap = service1.getService1Soap();
        return service1Soap.modifyInventory(inventory);
    }
    //********************************发运********************************************
    @BaseServiceDescribe(moduleName = "发运管理", btnName = "判断是否存在待拣货数据")
    public Integer dataJudgment(String sshzid, String ckid, List<String> listString)throws Exception {
        Map<String,Object> map=new HashMap<>();
        map.put("sshzid",sshzid);
        map.put("ckid",ckid);
        map.put("listString",listString);
        //i=1数据有待拣货数据；i=2没有待拣货数据
        Integer i=2;
        List<WmsShipmentOrder> list=dao.dataJudgment(map);
        if(list.size()>0){
            i=1;
        }
        return i;
    }
    /**
     * 发运
     *
     * @param sshzid
     * @param userinfo
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "发运管理", btnName = "发运")
    @Transactional
    public void despacthWmsicking(String sshzid, UserInfo userinfo, List<String> list) throws Exception {
        //查询货主信息
        WmsOwner wmsOwner = dao.searchWmsOwner(sshzid);
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        String username=userinfo.getUser().getName();
        Map<String, Object> map = null;
        Map<String, Object> map1 = null;
        Map<String, Object> map2 = null;
        List<String> stringList2 = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        stringList.add("1");
        stringList.add("2");
        Inventory inventory=null;
        for (String id : list) {
            long incr=redisUtils.incr("发运"+id);
            long ins=1;
            if(incr!=ins){
                redisUtils.remove("发运"+id);
                throw new RuntimeException("正在等待操作请稍后刷新重试。。。");
            }
            Integer dd = 0;
            //查询发运订单数据
            WmsShipmentOrder wmsShipmentOrder = dao.selectDd(id);
            //拣货状态（0、初始 1、部分拣货 2、全部拣货）
            String jhzt = "1";
            String jhzt2 = "2";
            if (!jhzt.equals(wmsShipmentOrder.getJhzt()) && !jhzt2.equals(wmsShipmentOrder.getJhzt())) {
                redisUtils.remove("发运"+id);
                throw new RuntimeException("请选择拣货状态为部分拣货或者全部拣货的发运订单数据");
            } else {
                //状态(0、初始 1、部分分配2、全部分配 3、部分发运4、全部发运5、订单取消6、订单完成)
                String fyzt = "1";
                String fyzt2 = "2";
                if (!fyzt.equals(wmsShipmentOrder.getFyzt()) && !fyzt2.equals(wmsShipmentOrder.getFyzt())) {
                    redisUtils.remove("发运"+id);
                    throw new RuntimeException("请选择发运订单状态是部分分配或全部分配的发运订单数据");
                } else {
                    //用于标记发运订单明细发运状态(true为部分发运,false为全部发运)
                    Integer flag = 0;
                    map = new HashMap<>();
                    map.put("ssfyddid", wmsShipmentOrder.getId());
                    map.put("jhzt", stringList);
                    //查询发运订单明细
                    List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed(map);
                    //记录未发运数据
                    Integer fysl=0;
                    for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                        //用来比对明细数量的,判断部分发运还是全部发运
                        Integer zsl = 0;
                        map = new HashMap<String, Object>();
                        map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                        map.put("ckid", ckid);
                        //发运状态 0、初始
                        map.put("fyzt", "0");
                        //发运任务下发状态 0：初始 1：已下发
                        map.put("xfzt", "0");
                        //出库复核 0否 1是
                        if ("1".equals(wmsOwner.getCkfh())) {
                            //查拣货集合
                            List<WmsPicking> wmsPickingList = dao.seachJh(map);
                            for (WmsPicking wmsPicking : wmsPickingList) {
                                Integer zsl2 = 0;
                                //复核状态 0：未复核 1：已复核
                                if ("1".equals(wmsPicking.getFhzt())) {
                                    zsl = zsl + wmsickingFz(zsl2, wmsPicking, map2,inventory,username);
                                } else if (!"0".equals(wmsPicking.getSszxid())) {
                                    //整箱的拣货记录将不需要复核就可以发运(查询是否为整箱)
                                    WmsBox wmsBox = dao.searchBox4(wmsPicking.getSszxid());
                                    //整散类型 0整箱 1散箱
                                    if ("0".equals(wmsBox.getZslx())) {
                                        zsl = zsl + wmsickingFz(zsl2, wmsPicking, map2,inventory,username);
                                    } else {
                                        redisUtils.remove("发运"+id);
                                        throw new RuntimeException(wmsShipmentOrder.getFybh() + "发运订单散箱未复核");
                                    }
                                } else {
                                    redisUtils.remove("发运"+id);
                                    throw new RuntimeException(wmsShipmentOrder.getFybh() + "发运订单未复核成功");
                                }
                            }
                        } else {
                            List<WmsPicking> wmsPickingList = dao.seachJh(map);
                            for (WmsPicking wmsPicking : wmsPickingList) {
                                Integer zsl2 = 0;
                                zsl = zsl + wmsickingFz(zsl2, wmsPicking, map2,inventory,username);
                            }
                        }
                        //发运总数量
                        Integer jhsl = Integer.valueOf(wmsShipmentOrderDetailed.getJhsl());
                        //货品对应的箱包装zdwsl对应主单位数量
                        map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                        map.put("bzlx", "2");
                        WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                        map.put("bzlx", "0");
                        WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                        //箱主单位整数（箱数）
                        Integer xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                        //箱主单位余数（个数）
                        Integer xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                        //明细fyxdwsl内的（发运）箱单位数量
                        String fyxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                        map.put("mxid", wmsShipmentOrderDetailed.getId());
                        map.put("fyxdwsl", fyxdwsl);
                        map.put("zsl", zsl);
                        String fymxzt;
                        Integer mx = 0;
                        Integer zsl2 = 0;
                        if (zsl2.equals(zsl)) {
                            flag = 4;
                            fysl++;
                        } else if (zsl < jhsl) {
                            map.put("fymxzt", "5");
                            mx = dao.updateMxfyzt(map);
                            flag = 5;
                            if (mx > 0) {
                            } else {
                                redisUtils.remove("发运"+id);
                                throw new RuntimeException("发运失败");
                            }
                        } else {
                            map.put("fymxzt", "6");
                            mx = dao.updateMxfyzt(map);
                            flag = 6;
                            if (mx > 0) {
                            } else {
                                redisUtils.remove("发运"+id);
                                throw new RuntimeException("发运失败");
                            }
                        }
                    }
                    String fyzt1="5";
                    String fyzt3="6";
                    map = new HashMap<String, Object>();
                    map.put("fyzt1",fyzt1);
                    map.put("orderid", wmsShipmentOrder.getId());
                    Integer fysl1=dao.searchDdMxSl1(map);
                    map.put("fyzt1",fyzt3);
                    Integer fysl3=dao.searchDdMxSl1(map);
                    //改订单状态
                    String fyddzt;
                    Integer flag2 = 6;
                    Integer flag3 = 5;
                    if(fysl1>0 | fysl3>0){
                        if (fysl1>0) {
                            //部分发运
                            map.put("fyddzt", "3");
                            dd = dao.updateDdfyzt(map);
                            if (dd > 0) {
                                redisUtils.remove("发运"+id);
                                stringList2.add(id);
                            } else {
                                redisUtils.remove("发运"+id);
                                throw new RuntimeException("发运失败");
                            }
                        }else if (flag2.equals(flag)) {
                            //全部发运
                            map.put("fyddzt", "4");
                            dd = dao.updateDdfyzt(map);
                            if (dd > 0) {
                                redisUtils.remove("发运"+id);
                                stringList2.add(id);
                            } else {
                                redisUtils.remove("发运"+id);
                                throw new RuntimeException("发运失败");
                            }
                        }
                    }else{
                        redisUtils.remove("发运"+id);
                    }
                }
            }
        }
        log.info("订单集合的大小:"+stringList2.size());
        if (stringList2.size() > 0) {
            for (String id : stringList2) {
                //传发运东单id查询装箱信息(发送短信专用按照客户发短信)
                List<WmsBox> wmsBoxList = dao.searchBoxMx(id);
                for (WmsBox wmsBox : wmsBoxList) {
                    if(wmsBox.getYsdh()==null){
                        wmsBox.setYsdh("");
                    }
                    Map<String, Object> map3 = HttpUtil.message(wmsBox.getLxrxm(), wmsBox.getLxrdh(), wmsBox.getSobh(), wmsBox.getYsdh(),wmsBox.getCyslb());
                    boolean flag = (boolean) map3.get("type");
                    if (!flag) {
                        if (!"".equals(map3.get("msg")) && map3.get("msg") != null) {
                            throw new ServiceException("短信发送失败:" + map3.get("msg").toString());
                        } else {
                            throw new ServiceException("短信发送失败");
                        }
                    }
                }
            }
        }
        throw new SucessException("发运成功");
    }

    /**
     * 辅助
     *
     * @param zsl2
     * @param wmsPicking
     * @param map2
     * @return Integer
     * @throws Exception
     */
    @Transactional
    public Integer wmsickingFz(Integer zsl2, WmsPicking wmsPicking, Map<String, Object> map2,Inventory inventory,String username) throws Exception {
        //叠加实捡数量
        zsl2 = Integer.parseInt(wmsPicking.getSjsl());
        //用库存id和集货库位id查 新库存返回库存对象
        map2 = new HashMap<String, Object>();
        map2.put("jhhkcid", wmsPicking.getJhhkcid());
        map2.put("jihkwid", wmsPicking.getJihkwid());
        WmsStock wmsStock = dao.selectStock(map2);
        if (wmsStock != null) {
            map2.put("id", wmsPicking.getId());
            //1、部分分配
            map2.put("fyzt", "1");
            map2.put("fyr", username);
            //更改拣货表发运状态为已发运
            Integer i = dao.updateJhzt(map2);
            if (i > 0) {
                int sl=Integer.valueOf(wmsStock.getSl());
                map2.put("scid", wmsStock.getId());
                map2.put("sl", "0");
                //再更改新库存的数量
                Integer j = dao.updateNewKcsl(map2);
                Integer j2 = 0;
                if (j > j2) {
                    log.info("发运操作(库存id:" + wmsStock.getId() + ";原库存数量:" + wmsStock.getSl() + ")");
                } else {
                    redisUtils.remove("发运"+wmsPicking.getSsydid());
                    throw new RuntimeException("更改理货库位数量失败");
                }
            } else {
                redisUtils.remove("发运"+wmsPicking.getSsydid());
                throw new RuntimeException("拣货表发运状态更改失败");
            }
        } else {
            redisUtils.remove("发运"+wmsPicking.getSsydid());
            throw new RuntimeException("理货库位数量为空");
        }
        return zsl2;
    }

    /**
     * 追加发运
     * @param sshzid
     * @param userinfo
     * @param list
     * @throws ServiceException
     */
    @BaseServiceDescribe(moduleName = "追加发运管理",btnName = "追加发运")
    @Transactional
    public void despacthAddWmsicking( String sshzid,UserInfo userinfo,List<String> list) throws Exception {
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//取仓库id
        //查询货主信息
        WmsOwner wmsOwner = dao.searchWmsOwner(sshzid);
        String username=userinfo.getUser().getName();
        Map<String, Object> map = null;
        Map<String, Object> map2 = null;
        //判断货主管理中“是否切箱” zxqx 0不切箱 1切箱,  dcfh是否允许多次发货 0不允许 1允许 默认1
        WmsPicking owner = dao.sechSfqx(sshzid);
        List<String> stringList2 = new ArrayList<>();
        List<String> stringList = new ArrayList<>();
        stringList.add("5");
        stringList.add("4");
        Inventory inventory=null;
        if ("1".equals(owner.getDcfh())) {
            for (String id : list) {
                //记录未发运数据
                Integer fysl=0;
                Integer dd = 0;
                //发运订单
                WmsShipmentOrder wmsShipmentOrder = dao.selectDd(id);
                //3、部分发运
                String fyzt = "3";
                if (!fyzt.equals(wmsShipmentOrder.getFyzt())) {
                    throw new ServiceException("请选择发运订单状态是部分发运的发运订单数据");
                } else {
                    //用于标记发运订单明细发运状态(true为部分发运,false为全部发运)
                    Integer flag = 0;
                    //查询发运订单明细用订单id
                    map = new HashMap<>();
                    map.put("ssfyddid", wmsShipmentOrder.getId());
                    //5、部分发运
                    map.put("fymxzt", stringList);
                    //发运订单明细查询
                    List<WmsShipmentOrderDetailed> wmsShipmentOrderDetailedList = dao.searchOrderDetailed1(map);
                    for (WmsShipmentOrderDetailed wmsShipmentOrderDetailed : wmsShipmentOrderDetailedList) {
                        Integer zsl = 0;//用来比对明细数量的,判断部分发运还是全部发运
                        //拿订单明细id查拣货集合
                        map = new HashMap<String, Object>();
                        map.put("ssfymxid", wmsShipmentOrderDetailed.getId());
                        map.put("ckid", ckid);
                        //发运状态 0初始 1复核成功 2复核失败
                        map.put("fyzt", "0");
                        //发运任务下发状态 0：初始 1：已下发
                        map.put("xfzt", "0");
                        //查拣货集合
                        List<WmsPicking> wmsPickingList = dao.seachJh(map);
                        for (WmsPicking wmsPicking : wmsPickingList) {
                            //出库复核 0否 1是
                            if ("0".equals(wmsOwner.getCkfh())) {
                                zsl += wmsickingFz(zsl, wmsPicking, map2,inventory,username);
                            } else if ("1".equals(wmsOwner.getCkfh())) {
                                //复核状态 0：未复核 1：已复核
                                if ("1".equals(wmsPicking.getFhzt())) {
                                    zsl += wmsickingFz(zsl, wmsPicking, map2,inventory,username);
                                } else if (!"0".equals(wmsPicking.getSszxid()) && "0" != wmsPicking.getSszxid()) {
                                    //整箱的拣货记录将不需要复核就可以发运(查询是否为整箱)
                                    WmsBox wmsBox = dao.searchBox4(wmsPicking.getSszxid());
                                    //整散类型 0整箱 1散箱
                                    if ("0".equals(wmsBox.getZslx())) {
                                        zsl += wmsickingFz(zsl, wmsPicking,map2,inventory,username);
                                    }
                                }
                            }
                        }
                        //发运总数量
                        Integer jhsl = Integer.valueOf(wmsShipmentOrderDetailed.getJhsl());
                        //每次累计明细里的拣货数量
                        zsl = zsl + Integer.parseInt(wmsShipmentOrderDetailed.getFysl());
                        //货品对应的箱包装zdwsl对应主单位数量
                        map.put("hpid", wmsShipmentOrderDetailed.getHpid());
                        map.put("bzlx", "2");
                        WmsPackingDetailed detailed = dao.searchPackingDetailed(map);
                        map.put("bzlx", "0");
                        WmsPackingDetailed detailed2 = dao.searchPackingDetailed(map);
                        //箱主单位整数（箱数）
                        Integer xdwzs = zsl / Integer.parseInt(detailed.getZdwsl());
                        //箱主单位余数（个数）
                        Integer xdwys = zsl % Integer.parseInt(detailed.getZdwsl());
                        //明细fyxdwsl内的（发运）箱单位数量
                        String jhxdwsl = xdwzs + detailed.getMs() + xdwys + detailed2.getMs();
                        map.put("mxid", wmsShipmentOrderDetailed.getId());
                        map.put("fyxdwsl", jhxdwsl);
                        map.put("zsl", zsl);
                        Integer zsl2 = 0;
                        if (zsl2.equals(zsl)) {
                            flag = 4;
                            fysl++;
                        } else if (zsl < jhsl) {
                            map.put("fymxzt", "5");
                            flag = 5;
                            Integer mx = dao.updateMxfyzt(map);
                            if (mx > 0) {
                            } else {
                                throw new ServiceException("追加发运失败");
                            }
                        } else {
                            map.put("fymxzt", "6");
                            flag = 6;
                            Integer mx = dao.updateMxfyzt(map);
                            if (mx > 0) {
                            } else {
                                throw new ServiceException("追加发运失败");
                            }
                        }


                    }
                    String fyzt1="5";
                    String fyzt3="6";
                    map = new HashMap<String, Object>();
                    map.put("fyzt1",fyzt1);
                    map.put("orderid", wmsShipmentOrder.getId());
                    Integer fysl1=dao.searchDdMxSl1(map);
                    map.put("fyzt1",fyzt3);
                    Integer fysl3=dao.searchDdMxSl1(map);
                    //改订单状态
                    String fyddzt;
                    Integer flag2 = 6;
                    Integer flag3 = 5;
                    if(fysl1>0 | fysl3>0){
                        if (fysl1>0) {
                            //部分发运
                            map.put("fyddzt", "3");
                            dd = dao.updateDdfyzt(map);
                            if (dd > 0) {
                                stringList2.add(id);
                            } else {
                                throw new ServiceException("发运失败");
                            }
                        }else if (flag2.equals(flag)) {
                            //全部发运
                            map.put("fyddzt", "4");
                            dd = dao.updateDdfyzt(map);
                            if (dd > 0) {
                                stringList2.add(id);
                            } else {
                                throw new ServiceException("发运失败");
                            }
                        }
                    }
                }
            }
            log.info("集合的大小:"+stringList2.size());
            if (stringList2.size() > 0) {
                for (String id : stringList2) {
                    //传发运东单id查询装箱信息(发送短信专用)
                    List<WmsBox> wmsBoxList = dao.searchBoxMx(id);
                    for (WmsBox wmsBox : wmsBoxList) {
                        if(wmsBox.getYsdh()==null){
                            wmsBox.setYsdh("");
                        }
                        Map<String, Object> map3 = HttpUtil.message(wmsBox.getLxrxm(), wmsBox.getLxrdh(), wmsBox.getSobh(), wmsBox.getYsdh(),wmsBox.getCyslb());
                        boolean flag = (boolean) map3.get("type");
                        if (!flag) {
                            if (!"".equals(map3.get("msg")) && map3.get("msg") != null) {
                                throw new ServiceException("短信发送失败:" + map3.get("msg").toString());
                            } else {
                                throw new ServiceException("短信发送失败");
                            }
                        }
                    }
                }
            }
            throw new SucessException("追加发运成功");
        }else{
            throw new ServiceException("请选择货主管理中'是否允许多次发货'为允许的数据");
        }
    }
}
